package cn.snailhd.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class FileUtils {
	private  List<File> fileList =new  ArrayList<File> ();


	public  List<File> getFileList(File file) {
		return getFileList(file,new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				return true;
			}
		});
	}

	public  List<File> getFileList(File file,FileFilter filter) {
		File[] files = file.listFiles(filter);
		
		if(files!=null){ //windows��Щ�ļ���java���ܷ��ʣ�file.listfiles()����Ϊnull
			for (File f : files) {
				
				if (f.isDirectory()) {
					getFileList(f.getAbsoluteFile(),filter);
				} else {
					//System.out.println(f +  "  count:  "+ count++);
					fileList.add(f);
				}
			}
		}
		return fileList;
	}


	
	

	public static Boolean copy(File file, File folder) {
		System.out.println("" + file.getName());
		if (file.exists()) {
			if (file.isDirectory()) {
				throw new RuntimeException("");
			}

			if (!folder.isDirectory()) {
				throw new RuntimeException("");
			}
			String copyFilePath = folder + "\\" + file.getName();

			if (new File(copyFilePath).exists()) {
				if (new File(copyFilePath).length() != file.length()) {
					String prefix = copyFilePath.substring(0, copyFilePath.lastIndexOf("."));
					String suffix = copyFilePath.substring(copyFilePath.lastIndexOf("."));
					copyFilePath = prefix + "_" + StringUtils.randomCode(4) + suffix;
				} else {
					return false;
				}

			}
			BufferedInputStream fi = null;
			BufferedOutputStream fo = null;
			try {
				fi = new BufferedInputStream(new FileInputStream(file)) ;
				fo = new BufferedOutputStream(new FileOutputStream(copyFilePath)) ;

				int num = 0;
				byte[] buff = new byte[1024 * 128];
				
				while ((num = fi.read(buff)) != -1) {
					fo.write(buff, 0, num);
					fo.flush();
				}
				/*
				//
				while ((num = fi.read()) != -1) {
					fo.write(num);
					fo.flush();
				}*/

			} catch (FileNotFoundException e) {
				System.out.println("");
			} catch (IOException e) {
				System.out.println("");
			} finally {
				IoUtils.close(fi);
				IoUtils.close(fo);
			}
			if (file.length() != new File(copyFilePath).length()) {
				System.out.println(file.getAbsolutePath() + " �ļ����Ʋ�����...ɾ��");
				return false;
			}
			return true;

		} else {
			return false;
		}

	}
	
	public static Boolean copy(String file, String folder) {
		return copy(new File(file),new File(folder));
	}
	
	
	

	public static boolean equalFile(String s1,String s2){
		return equalFile(new File(s1),new File(s2));
	}

	public static boolean equalFile(File f1,File f2){
		if(f1.length()==f2.length()){
			try {
				FileInputStream fis1 = new FileInputStream(f1);
				FileInputStream fis2 = new FileInputStream(f2);
				

				for(int i=0;i<100;i++){   
					if(fis1.read()!=fis2.read()){
						return false;
					}
				}				
				return true;		
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	
	
}
